package cn.net.qinling.xmlaccess;

import java.io.File;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import cn.net.qinling.healthcare.remote.RemoteGenerator;

public class TestDataGenerator implements StatusReporter {

	public static final int DEFAULT_COUNT = 45000;

	private int leftCount = 0;

	private static cn.net.qinling.xmlaccess.Timer operationTimer = new cn.net.qinling.xmlaccess.Timer();

	public TestDataGenerator() {
		// TODO Auto-generated constructor stub
	}

	public void report() {
		System.out.println(leftCount);
		System.out.println(operationTimer);
	}

	public static void main(String[] args) {
		TestDataGenerator gen = new TestDataGenerator();

		CollectStatusTask task = new CollectStatusTask();
		task.registerStatusReporter(gen);
		java.util.Timer timer = new java.util.Timer();
		timer.schedule(task, 1000, 1000);

		try {
			long startTime = System.currentTimeMillis();

			operationTimer.startTiming("Total");
			gen.generate(DEFAULT_COUNT);
			operationTimer.endTiming("Total");
			long endTime = System.currentTimeMillis();

			System.out.println("StartTime:\t" + new Date(startTime));
			System.out.println("EndTime:\t" + new Date(endTime));
			System.out.println(operationTimer);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			timer.cancel();
		}

	}

	public void testQuery() {
		try {
			DataAccess da = new OracleDataAccess();
			operationTimer.resumeTiming("Oracle");
			List<String> result = da.listRecordsByPatientId("");
			operationTimer.pauseTiming("Oracle");

			for (String rid : result) {
				System.out.println(rid);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void generate(int count) throws MalformedURLException,
			RemoteException, NotBoundException, SQLException {
		leftCount = count;

		try {
			DataAccess dao1 = new OracleDataAccess();
			DataAccess dao2 = new DB2DataAccess();
			RemoteGenerator gen = (RemoteGenerator) Naming
					.lookup("rmi://localhost:10991/cdagen");

			gen.enableRandomMode(false);

			int sourceId = -1;
			while (leftCount > 0) {
				String content = gen.next();
				if (null == content) {
					sourceId++;
					gen.feed("E:\\cda\\20123730101147_1000000\\" + sourceId
							+ File.separator);
					continue;
				}

				String id = UUID.randomUUID().toString();

				operationTimer.resumeTiming("Oracle");
				dao1.insert(id, content);
				operationTimer.pauseTiming("Oracle");
				
				operationTimer.resumeTiming("DB2");
				dao2.insert(id, content);
				operationTimer.pauseTiming("DB2");				

				leftCount--;
			}
		} catch (MalformedURLException | RemoteException | NotBoundException
				| SQLException ex) {

			ex.printStackTrace();
		}

	}
}
